<?php
/**
 * 标签管理
 *
 * User: kaikaic
 * Date: 2018/4/19
 * Time: 23:01
 */

namespace app\exam\controller;

use app\exam\model\TagsModel;
use controller\BasicAdmin;
use service\DataService;
use service\TagsService;
use think\Db;

class Tags extends BasicAdmin {

    protected $table = "SystemTags";

    protected $categoryKey = "category_tags";

    public function index() {
        TagsModel::$categorys = $this->readCategoryConfig();
        $tagList = TagsModel::combineTagsByCategory();

        $this->assign("title", "标签管理");
        $this->assign("tag_list", $tagList);
        return view();
    }

    /**
     * 新建-标签
     */
    public function add() {
        $category = $this->readCategoryConfig();

        if ($this->request->isGet()) {
            return view('form', ['title' => '新建标签', 'category_list'=>$category]);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (($ids = $this->doInsertAction($data)) && !empty($ids)) {
                $post = ['tag_id' => $ids, 'create_by' => session('user.id')];
                if (DataService::save($this->table, $post, 'id') !== false) {
                    $this->success('标签添加成功！', '');
                }
            }

            $this->error('标签添加失败，请稍候再试！');
        }
    }

    /**
     * 编辑-标签
     */
    public function edit() {
        $id = $this->request->param('id', '');
        if (empty($id)) {
            $this->error('参数错误，请稍候再试！');
        }

        $tag = TagsService::find($id);
        if (empty($tag)) {
            $this->error('记录不存在，请重新操作!');
        }

        if ($this->request->isGet()) {
            $category = $this->readCategoryConfig();

            return view('form', [
                'title'=>'编辑标签',
                'vo'=>$tag,
                'category_list'=>$category
            ]);
        }

        if ($this->request->isPost()) {
            $id = $this->request->post('id', '');
            if (empty($id)) {
                $this->error('参数错误，请稍候再试！');
            }

            $data = $this->request->post();
            if (($ids = $this->doInsertAction($data)) && !empty($ids)) {
                $this->success('编辑成功！', '');
            }else {
                $this->error('编辑失败，请稍候再试！');
            }
        }
    }

    /**
     * 删除-标签
     */
    public function del() {
        if (DataService::update($this->table)) {
            $this->success("删除标签成功！", '');
        }
        $this->error("删除标签失败，请稍候再试！");
    }

    /**
     * 数据写入
     */
    protected function doInsertAction($data, $ids = []) {
        if (empty($data['id'])) {
            $data['create_time'] = time();

            $result = $id = Db::name($this->table)->insertGetId($data);
        } else {
            $id = intval($data['id']);
            $result = Db::name($this->table)->where('id', $id)->update($data);
        }
        if ($result !== FALSE) {
            $ids[] = $id;
        }

        return join(',', $ids);
    }

    /**
     * 列表数据处理
     */
    protected function _index_data_filter(&$data) {
        $size = count($data);

        // 分类配置
        $category = $this->readCategoryConfig();
        for ($i=0; $i<$size; $i++) {
            $data[$i]['category_txt'] = $category[$data[$i]['category_id']];
        }

        $this->assign("category_list", $category);
    }
}